package com.xinfu.newproject.model.certifition;

import android.util.Log;

import com.google.gson.Gson;
import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.certifition.AddRoomBean;
import com.xinfu.newproject.bean.certifition.AddRoomBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddRoomModel implements IModel.IAddRoomModel {

    @Override
    public void addRoom(AddRoomBody addRoomBody, MyCallBack<AddRoomBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", addRoomBody.getProjectId());
        map.put("userId", addRoomBody.getUserId());
        map.put("userName", addRoomBody.getUserName());
        map.put("roomId", addRoomBody.getRoomId());
        map.put("applyName", addRoomBody.getApplyName());
        map.put("applyTel", addRoomBody.getApplyTel());
        map.put("address", addRoomBody.getAddress());
        map.put("idType", addRoomBody.getIdType());
        if (addRoomBody.getImgUrl() != null) {
            map.put("imgUrl", addRoomBody.getImgUrl());
            addRoomBody.setApplyCusName(null);
            addRoomBody.setApplyCusTel(null);
            addRoomBody.setArea(null);
        }
        if (addRoomBody.getApplyCusName() != null && !addRoomBody.getApplyCusName().equals("")) {
            map.put("applyCusName", addRoomBody.getApplyCusName());
            map.remove("imgUrl");
            addRoomBody.setArea(null);
        }
        if (addRoomBody.getApplyCusTel() != null && !addRoomBody.getApplyCusTel().equals("")) {
            map.put("applyCusTel", addRoomBody.getApplyCusTel());
            map.remove("imgUrl");
            addRoomBody.setArea(null);
        }
        if (addRoomBody.getArea() != null && !addRoomBody.getArea().equals("")) {
            map.put("area", addRoomBody.getArea());
            map.remove("imgUrl");
            map.remove("applyCusTel");
            map.remove("applyCusName");
        }
        Log.i("testAddRoom", new Gson().toJson(map));
        JsNetworkService.getInstance().getIJsNetworkService()
                .addRoomData(addRoomBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddRoomBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull AddRoomBean addRoomBean) {
                        callBack.onSuccess(addRoomBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
